package com.example.springmysqldemo.dao;

        import com.example.springmysqldemo.entity.Book;
        import org.apache.logging.log4j.message.ReusableMessage;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.jdbc.core.BeanPropertyRowMapper;
        import org.springframework.jdbc.core.JdbcTemplate;
        import org.springframework.stereotype.Repository;

        import java.util.List;

@Repository
public class BookDaoimpl implements BookDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Book findBookInfo(String id) {
        String sql = "select * from t_book where userId = ?";
        Book book = jdbcTemplate.queryForObject(sql,
                new BeanPropertyRowMapper<Book>(Book.class),id);
        return book;
    }

    @Override
    public List<Book> findAllBook() {
        String sql = "select * from t_book";
        List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
        return bookList;
    }

    @Override
    public String deleteBook(String id) {
        String sql = "delete  from t_book where userId = ?";
        int update = jdbcTemplate.update(sql, id);
        if (update == 0 ) {
            return "0";
        }
        else {
            return "1" ;
        }
    }

    @Override
    public String addBook(String username, String ustatus) {
        String sql = "insert into t_book (username,ustatus) values (?,?)";
        Object [] args = {username,ustatus};
        int update = jdbcTemplate.update(sql, args);
        if (update == 0){
            return "0";
        }else{
            return "1";
        }
    }

    @Override
    public String editBook(String username, String ustatus, String id) {
        String sql = "update t_book set username = ? , ustatus = ? where userId = ? ";
        Object [] args = {username,ustatus,id};
        int update = jdbcTemplate.update(sql, args);
        if (update == 0 ){
            return "0";
        }else {
            return "1";
        }
    }
}
